package com.base.commonlib.http.call;
import android.util.Log;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.callback.NavCallback;
import com.alibaba.android.arouter.launcher.ARouter;
import com.base.commonlib.base.BaseApp;
import com.base.commonlib.bean.CardResult;
import com.base.commonlib.http.BaseResponse;
import com.base.commonlib.http.CallBack;
import com.base.commonlib.http.CallBackRtsp;
import com.base.commonlib.http.NetWorkInitial;
import com.base.commonlib.http.ResponseT;
import com.base.commonlib.http.utils.CallBackImpl;
import com.base.commonlib.http.utils.ParameterizedTypeImpl;
import com.base.commonlib.utils.ToastUtil;
import com.google.gson.Gson;

import java.lang.reflect.Type;
import java.util.List;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Response;

/**
 * Date：2019/3/28 下午10:32
 * Creator：arvin
 * Des：
 */

public class DataRequestCall extends BaseRequestCall {


    protected Flowable<Response<String>> flowable;


    public DataRequestCall(Flowable<Response<String>> flowable) {
        this.flowable = flowable;
    }


    public <T> Disposable enqueue(final Class<T> clazz , final CallBackImpl callBack) {

        return flowable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .retry(1)
                .subscribe(new Consumer<Response<String>>() {
                    @Override
                    public void accept(Response<String> response) throws Exception {
                        ParameterizedTypeImpl type = new ParameterizedTypeImpl(BaseResponse.class, new Class[]{clazz});
                        String body = NetWorkInitial.customDecode(response);
                        Gson gson = new Gson();
                        if (body.equals("[]")){
                            callBack.onError(-1, "暂无数据", null);
                        }else {
                            BaseResponse<T> mBaseResponse = gson.fromJson(body, type);
                            if (mBaseResponse !=null){
                                if (mBaseResponse.getStatus() == 1) {
                                    callBack.onResponse(mBaseResponse.getData());
                                } else if (mBaseResponse.getStatus() == 0) {
                                    if (mBaseResponse.getCode()==102){
                                        //token失效
                                        callBack.onError(-1, mBaseResponse.getMsg(), null);
                                        ARouter.getInstance().build("/mine/pwdlogin").navigation();
                                    }else if (mBaseResponse.getCode()==108){
                                        //token失效
                                        callBack.onError(-1, mBaseResponse.getMsg(), null);
                                    }else if (mBaseResponse.getCode() ==114){
                                        callBack.onError(-1, mBaseResponse.getMsg(), null);
                                    }else {
                                        callBack.onError(-1, mBaseResponse.getMsg(), null);
                                    }
                                } else {
                                    callBack.onError(-1, mBaseResponse.getMsg(), null);
                                }
                            }else {
                                callBack.onError(-1,"系统出错...","系统出错...");
                            }

                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        Log.e("exception",throwable.toString()+"");
                        callBack.onError(-1,"系统出错...",null);
                    }
                });


    }

    public <T> Disposable enqueueForCard(final Class<T> clazz , final CallBackImpl callBack) {

        return flowable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .retry(1)
                .subscribe(new Consumer<Response<String>>() {
                    @Override
                    public void accept(Response<String> response) throws Exception {
                        ParameterizedTypeImpl type = new ParameterizedTypeImpl(CardResult.class, new Class[]{clazz});
                        String body = NetWorkInitial.customDecode(response);
                        Gson gson = new Gson();
                        CardResult<T> mBaseResponse = gson.fromJson(body,type);
                        if (mBaseResponse.isSuccess()){
                            callBack.onResponse(mBaseResponse.getResults());
                        }else {
                            callBack.onError(-1,mBaseResponse.getMsg()+"",null);
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        //callBack.onError(-1,throwable.toString(),null);
                        Log.e("exception",throwable.toString()+"");
                        callBack.onError(-1,"系统出错...",null);
                    }
                });


    }
    public <T> Disposable enqueue(final Class<T> clazz, final CallBack<T> callBack) {

        return flowable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .retry(1)
                .subscribe(new Consumer<Response<String>>() {
                    @Override
                    public void accept(Response<String> response) throws Exception {
                        String body = NetWorkInitial.customDecode(response);
                        ParameterizedTypeImpl type = new ParameterizedTypeImpl(ResponseT.class, new Class[]{clazz});
                        Gson gson = new Gson();
                        ResponseT<T> responseT = gson.fromJson(body, type);
                        if(responseT.isSuccess())
                        {
                            if (responseT.getCode() == 401){
                                callBack.onLogin();
                            }else {
                                callBack.onResponse(responseT.getData());
                            }
                        }else
                        {
                            if (responseT.getCode() == 401) {
                                callBack.onLogin();
                            }else {
                                callBack.onError(responseT.getCode(), responseT.getMsg(), responseT.getErrMsg());
                            }
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        //callBack.onError(-1,null,null);
                        Log.e("exception",throwable.toString()+"");
                        callBack.onError(-1,"系统出错...",null);
                    }
                });
    }

    public <T> Disposable enqueueForArray(final Class<T> clazz, final CallBackImpl<List<T>> callBack) {

        return flowable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .retry(1)
                .subscribe(new Consumer<Response<String>>() {
                    @Override
                    public void accept(Response<String> response) throws Exception {
                        String body = NetWorkInitial.customDecode(response);
                        ParameterizedTypeImpl type_list = new ParameterizedTypeImpl(List.class, new Class[]{clazz});
                        ParameterizedTypeImpl type = new ParameterizedTypeImpl(BaseResponse.class, new Type[]{type_list});
                        Gson gson = new Gson();
                        BaseResponse<List<T>> mBaseResponse = gson.fromJson(body, type);
//                        BaseResponse<T> mBaseResponse = gson.fromJson(body,type);
                        if (mBaseResponse!=null){
                            if (mBaseResponse.getStatus() == 1){
                                if (mBaseResponse.getCode() == 401){
                                    callBack.login();
                                }else {
                                    callBack.onResponse(mBaseResponse.getData());
                                }

                            }else if (mBaseResponse.getStatus() == 0){
                                if (mBaseResponse.getCode() == 102){
                                    callBack.login();
                                }else if (mBaseResponse.getCode() == 99){
                                    callBack.onError(-1,mBaseResponse.getMsg(),null);
                                }else {
                                    callBack.onResponse(mBaseResponse.getData());
                                }
                            }else {
                                callBack.onError(-1,mBaseResponse.getMsg(),null);
                            }
                        }else {
                            callBack.onError(-1,"系统出错...","系统出错...");
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        //callBack.onError(-1,throwable.toString(),null);
                        Log.e("exception",throwable.toString()+"");
                        callBack.onError(-1,"系统出错...",null);
                    }
                });
    }



    public <T> Disposable enqueueRtsp(final CallBackRtsp callBack) {

        return flowable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .retry(1)
                .subscribe(new Consumer<Response<String>>() {
                    @Override
                    public void accept(Response<String> response) throws Exception {
                        String body = NetWorkInitial.customDecode(response);
                        callBack.onResponse(body);

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        callBack.onError(-1,throwable.toString(),null);
                    }
                });


    }
}
